package com.atXiao.food.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atXiao.food.config.Log;
import com.atXiao.food.constant.BusSignConstant;
import com.atXiao.food.constant.BuscodeConstant;
import com.atXiao.food.dao.RecipeDao;
import com.atXiao.food.entity.RecipeEntity;
import com.atXiao.food.rspbody.recipe.ReceiptIcon;
import com.atXiao.food.reqbody.receipt.SearchIconReq;
import com.atXiao.food.service.SearchIconService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SearchIconServiceImpl implements SearchIconService {

    public final static Log logger = new Log(SearchIconServiceImpl.class);


    @Autowired
    RecipeDao recipeDao;

    @Override
    public List<ReceiptIcon> searchIconList(String trace, SearchIconReq searchIconReq) {
        // 食谱标题
        String title = searchIconReq.getTitle();
        QueryWrapper<RecipeEntity> queryWrapper = new QueryWrapper<>();
        // 食谱标题参与模糊查询
        queryWrapper.like("title", title);
        // 列表数据
        List<RecipeEntity> recipeList = recipeDao.selectList(queryWrapper);
        List<ReceiptIcon> iconList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(recipeList)) {
            iconList = recipeList.stream().map(item -> {
                ReceiptIcon receiptIcon = new ReceiptIcon();
                BeanUtils.copyProperties(item, receiptIcon);
                return receiptIcon;
            }).collect(Collectors.toList());

        }
        logger.info(trace, BusSignConstant.TASTE_FOOD, BuscodeConstant.SEARCH_ICON_LIST, "icon搜索框查询信息:" + JSONObject.toJSONString(iconList));
        return iconList;
    }
}
